/**
 */
package com.dingpeikeji.quickdeer.core.modules.bus.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.service.CrudService;
import com.dingpeikeji.quickdeer.core.modules.bus.dao.TCompBusDao;
import com.dingpeikeji.quickdeer.core.modules.bus.entity.TCompBus;
import com.dingpeikeji.quickdeer.core.modules.realsche.dao.TRealscheBoardDao;
import com.dingpeikeji.quickdeer.core.modules.site.entity.TSitedtl;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Office;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.UserUtils;
import com.dingpeikeji.quickdeer.modules.rentproduct.dao.TCargotypeForsaleDao;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCargotype;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCargotypeForsale;;

/**
 * 巴士管理Service
 * 
 * @author Zhaoy
 * @version 2016-05-20
 */
@Service
@Transactional(readOnly = true)
public class TCompBusService extends CrudService<TCompBusDao, TCompBus> {

	@Autowired
	private TCargotypeForsaleDao cargotypeDao;

	public TCompBus get(String id) {
		return super.get(id);
	}

	public List<TCompBus> findList(TCompBus tCompBus) {
		return super.findList(tCompBus);
	}

	public Page<TCompBus> findPage(Page<TCompBus> page, TCompBus tCompBus) {
		return super.findPage(page, tCompBus);
	}

	// 增加可销售车型数量
	@Transactional(readOnly = false)
	public void addForsale(TCompBus tCompBus) {
		// 写入公司车型可销售表
		TCargotypeForsale cargosale = new TCargotypeForsale();

		// 读取原有车型可销售数量
		cargosale.setCargotype(new TCargotype());
		cargosale.getCargotype().setCargotypeid(tCompBus.getCartype().getCargotypeid());
		cargosale.setOffice(new Office());
		cargosale.getOffice().setId(tCompBus.getOffice().getId());

		List<TCargotypeForsale> salelist = cargotypeDao.findList(cargosale);

		if (salelist.size() == 0) {
			cargosale.setTotalnum("1");
			cargosale.setForsalenum("0");
			cargotypeDao.insert(cargosale);
		} else {
			cargosale = salelist.get(0);
			cargosale.setTotalnum(Integer.toString(Integer.parseInt(cargosale.getTotalnum()) + 1));
			// cargosale.setForsalenum(cargosale.getForsalenum() + 1);
			cargotypeDao.update(cargosale);
		}
	}
	
	//减少车型可销售数量
	@SuppressWarnings("deprecation")
	@Transactional(readOnly = false)
	public void subForsale(TCompBus oldbus) {
		// 改变旧车型
		TCargotypeForsale oldcargo = new TCargotypeForsale();
		// 查找该车队的可销售数量表
		oldcargo.setCargotype(new TCargotype());
		oldcargo.getCargotype().setCargotypeid(oldbus.getCartype().getCargotypeid());
		oldcargo.setOffice(new Office());
		oldcargo.getOffice().setId(oldbus.getOffice().getId());

		List<TCargotypeForsale> tmpsale = cargotypeDao.findList(oldcargo);
		if (tmpsale.size() > 0) {
			// 如果有此车型，则车型车辆总数-1
			oldcargo = tmpsale.get(0);
			oldcargo.setTotalnum(Integer.toString(Integer.parseInt(oldcargo.getTotalnum()) - 1));
			if (Integer.parseInt(oldcargo.getTotalnum()) <= 0){
				cargotypeDao.delete(String.valueOf(oldcargo.getForsaleid()));
			}
			if (Integer.parseInt(oldcargo.getForsalenum()) > 0) {
				oldcargo.setForsalenum(Integer.toString(Integer.parseInt(oldcargo.getForsalenum()) - 1));
				cargotypeDao.update(oldcargo);
			}
		}
	}

	@Transactional(readOnly = false)
	public void save(TCompBus tCompBus) {
		// 小于七座的为省际
		if (tCompBus.getSeats() <= 7) {
			tCompBus.setOperatearea(TCompBus.OPERATE_AREA_INTER);
		}

		if (tCompBus.getBusid() == null) {
			// 新增车辆
			super.save(tCompBus);

			// 新增可销售车型
			addForsale(tCompBus);
		} else {
			// 更新车辆记录

			// 获取原先车辆信息
			TCompBus oldbus = super.get(tCompBus.getBusid());

			// 如果改变车型 可销售数量原车型-1，新车型+1
			if (oldbus.getCartype().getCargotypeid() != tCompBus.getCartype().getCargotypeid()) {
				subForsale(oldbus);
				addForsale(tCompBus);
			}
			
			super.save(tCompBus);
		}

	}

	@Transactional(readOnly = false)
	public void delete(TCompBus tCompBus) {
		super.delete(tCompBus);
	}

	public List<TCompBus> findLists(TCompBus tCompBus) {
		return super.findLists(tCompBus);
	}

	public List<TCompBus> findListse(TCompBus tCompBus) {
		return super.findListse(tCompBus);
	}

	public List<TCompBus> findListOffice(String ids) {
		return super.findListOffice(ids);
	}

	/**
	 * 根据车牌号获取巴士
	 * 
	 * @param licenseno
	 * @return
	 */
	public TCompBus getBusByLicenseno(String licenseno) {
		return dao.getBusByLicenseno(licenseno);
	}
}
